package ru.tulupov.scheduledrunner;

import java.util.Arrays;

import ru.tulupov.scheduledrunner.command.Command;
import ru.tulupov.scheduledrunner.command.CommandFactory;
import ru.tulupov.scheduledrunner.command.Context;
import ru.tulupov.scheduledrunner.command.Parser;
import ru.tulupov.scheduledrunner.util.ThreadPool;

/**
 * 
 * @author <a href="mailto:zhekka3@gmail.com">Eugene Tulupov</a>
 */
public class Main {

	private static final int COUNT = 10;

	public static void main(String[] args) {
		
		System.out.println("Scheduled runner");
		System.out.println("Type 'help' for viewing information about available commands");
		System.out.println();
		System.out.print("# ");
		
		ThreadPool threadPool = new ThreadPool(COUNT);
		Parser parser = new Parser(System.in);
		CommandFactory factory = CommandFactory.getInstance();
		Context context = new Context();
		context.setThreadPool(threadPool);
		Parser.Result result = null;
		while ((result = parser.next()) != null) {
			Command command = factory.get(result.getName());
			if (command != null) {
				command.init(result.getArgs());
				try {
					command.execute(context);
				} catch (Exception e) {
					System.err.println("Invalid command!");
				}
			} else {
				System.err.println("Invalid command!");
			}
			System.out.print("# ");
		}
	}

}
